﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;
using IQKS.AWP.BusinessAccessLayer;
using IQKS.AWP.BusinessObject;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtEmailID.Focus();
    }
    protected void cmdLogin_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (Membership.ValidateUser(Membership.GetUserNameByEmail(txtEmailID.Text.Trim()), txtPassword.Text.Trim()) == true)
            {
                string[] strRoleName;

                FormsAuthentication.RedirectFromLoginPage(Membership.GetUserNameByEmail(txtEmailID.Text.Trim().ToString()).ToString(), false);
                strRoleName = Roles.GetRolesForUser(Membership.GetUserNameByEmail(txtEmailID.Text.Trim().ToString()).ToString());
                Session["UserName"] = txtEmailID.Text.Trim();
                Response.Redirect("~/home/DashBoard.aspx");
                //switch (strRoleName[0])
                //{
                //    case "SU":
                //        Response.Redirect("~/home/Dashboard.aspx");
                //        break;
                //    case "GD":
                //        Response.Redirect("~/regionaldirector/rdhome_1.aspx");
                //        break;
                //    case "RD":
                //        Response.Redirect("~/regionaldirector/rdhome_1.aspx");
                //        break;
                //    case "RARL":
                //        Response.Redirect("~/ResearchLead/rdhome_1.aspx");
                //        break;
                //    case "RAM":
                //        Response.Redirect("~/Moderator/rdhome_1.aspx");
                //        break;
                //    case "AD":
                //        Response.Redirect("~/Auditor/rdhome_1.aspx");
                //        break;
                //    case "IE":
                //        Response.Redirect("~/IndExpert/rdhome_1.aspx");
                //        break;
                //    case "IQKSAD":
                //        //Response.Redirect("~/IQManager/rdhome_1.aspx");
                //        Response.Redirect("home.aspx");
                //        break;
                //    default:
                //        Response.Redirect("~/Candidate/CandidateBrief.aspx");
                //        break;
                //}
            }
            else
            {
                // Determine why the user could not login...
                ErrorMsg.Text = "Invalid Email ID or Password. Please try again.";

                // Does there exist a User account for this user?
                string str = Membership.GetUserNameByEmail(txtEmailID.Text.Trim());

                if (str != null)
                {
                    MembershipUser usrInfo = Membership.GetUser(Membership.GetUserNameByEmail(txtEmailID.Text.Trim()));
                    if (usrInfo != null)
                    {
                        // Is this user locked out?
                        if (usrInfo.IsLockedOut)
                        {
                            ErrorMsg.Text = "Your account has been locked, Kindly use forgot password to generate your new password.";
                        }
                        else if (!usrInfo.IsApproved)
                        {
                            ErrorMsg.Text = "You can not use this option to login till your payment is received in our system and internally approved. Please login by entering your 'REGISTRATION NUMBER' in the separate login option for 'NEWLY REGISTERED' provided below.";
                        }
                    }
                }
                else
                {
                    ErrorMsg.Text = "Your Email ID is not yet registered. Please proceed with new Registration.";
                }
            }
        }
    }

    protected void Btnregister_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Common/CandidateRegistration.aspx");
    }
    protected void Btnalreadyregister_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Common/LoginAlready.aspx");
    }
    protected void btnRegLogin_Click(object sender, EventArgs e)
    {
        string UserEmailid = txtSecondEmail.Text.Trim();
        string UserRegID = txtSecondRegID.Text.Trim();
        DataSet objDs = new DataSet();
        string EmailId = string.Empty;
        string RegstrationId = string.Empty;
        string strUserID = string.Empty;
        string strCandidateID = string.Empty;
        Session["CandidateUser"] = UserEmailid;
        objDs = GetUserLoginDataSet(UserEmailid, UserRegID);

        if (objDs.Tables.Count > 0)
        {
            if (objDs.Tables[0].Rows.Count > 0)
            {
                EmailId = Convert.ToString(objDs.Tables[0].Rows[0]["UserName"]);
                RegstrationId = Convert.ToString(objDs.Tables[0].Rows[0]["RegistrationNo"]);
                strUserID = Convert.ToString(objDs.Tables[0].Rows[0]["UserId"]);
                strCandidateID = Convert.ToString(objDs.Tables[0].Rows[0]["CandidateID"]);

                if (EmailId == UserEmailid && RegstrationId == UserRegID)
                {
                    //session for pass below page.
                    Session["CandidateUserID"] = strUserID.Trim();
                    Session["CandidateID"] = strCandidateID.Trim();
                    Response.Redirect("~/Common/RegisterAlready.aspx");

                }
                else
                {
                    lblReloginMsg.Text = "Invalid Email ID and Registration No.";
                    lblReloginMsg.ForeColor = System.Drawing.Color.Red;
                }

            }
            else
            {
                lblReloginMsg.Text = "Invalid Email ID and Registration No.";
                lblReloginMsg.ForeColor = System.Drawing.Color.Red;
                return;
            }

        }
        else
        {
            lblReloginMsg.Text = "Invalid Email ID and Registration No.";
            lblReloginMsg.ForeColor = System.Drawing.Color.Red;
            return;
        } 
    }
    private DataSet GetUserLoginDataSet(string Email_Id, string Regstration_Id)
    {
        DataSet objDs = null;
        try
        {
            objDs = new UserMasterBAL().GetUserLogin(Email_Id, Regstration_Id);

        }
        catch (Exception exception)
        {
            exception.AddErrorToLog(); //throw new ApplicationException(exception.Message);
        }
        return objDs;
    }
}